package com.example.myFirstWebmagic.pipeline;

import java.util.List;
import java.util.Map;

import com.example.myFirstWebmagic.hourAirReport.dao.HourAirQualityReportRepository;
import com.example.myFirstWebmagic.hourAirReport.entity.EnHourAirQualityReport;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

/**
 * 
 * ClassName: MysqlPipeline 
 * @Description: 爬虫结果保存到数据库
 * @author xiongyao
 * @date 2017年2月28日
 */

public class HourMysqlPipeline implements Pipeline {
	
	private final HourAirQualityReportRepository hourAirQualityReportRepository;
	
	public HourMysqlPipeline(HourAirQualityReportRepository hourAirQualityReportRepository){
		this.hourAirQualityReportRepository=hourAirQualityReportRepository;
	}
	
	
	@Override
	public void process(ResultItems resultItems, Task task) {
		// TODO Auto-generated method stub
        //遍历所有结果，保存到数据库
        for (Map.Entry<String, Object> entry : resultItems.getAll().entrySet()) {
            @SuppressWarnings("unchecked")
			List<EnHourAirQualityReport> list= (List<EnHourAirQualityReport>) entry.getValue();
            for(EnHourAirQualityReport report:list){
            	
            	hourAirQualityReportRepository.save(report);
            }
            
        }

	}

}
